package com.example.ljy;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.ljy.db.UserDao;
import com.example.ljy.model.News;
import com.github.mikephil.charting.charts.LineChart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QuXianActivity extends AppCompatActivity {
    UserDao userDao;
    List<Float> list =new ArrayList<>();
    TextView hight;
    TextView lower;
    //定义画图框变量
    private LineChartFunction dynamicLineChartManager1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quxian_layout);
        hight = findViewById(R.id.hight);
        lower = findViewById(R.id.lower);

        userDao = UserDao.getInstance(getApplicationContext());
        List<Map<String, ?>> allUsers = userDao.getAllUsers();
        if(allUsers!=null&&allUsers.size()>0){
            for (int i = 0; i < allUsers.size(); i++) {
                Map<String, ?> stringMap = allUsers.get(i);
                list.add(Float.valueOf((String) stringMap.get("temperature")));
                /*News news = new News();
                news.title= (String) stringMap.get("time");
                news.content= (String) stringMap.get("temperature");
                mNewsList.add(news);*/
            }
        }
        //寻找控件ID
        final LineChart mChart1 = findViewById(R.id.dynamic_chart1);
        final Button button_data1 = findViewById(R.id.button_data1);
        dynamicLineChartManager1 = new LineChartFunction(mChart1, "温度显示", Color.GREEN);
        dynamicLineChartManager1.setYAxis(40, 5, 10);//最大值，最小值，中间刻度值的数量
        dynamicLineChartManager1.setLimte(35);
        if(list==null||list.size()==0){return;}

        for (int i = 0; i < list.size(); i++) {
            dynamicLineChartManager1.addEntry(list.get(i));
        }
        Float max = list.get(0);
        Float min = list.get(0);


        // store the length of the listayList in variable n

        int n = list.size();

        // loop to find minimum from listayList
        for (int i = 1; i < n; i++) {

            if (list.get(i) < min) {

                min = list.get(i);

            }

        }

        // loop to find maximum from listayList

        for (int i = 1; i < n; i++) {

            if (list.get(i) > max) {

                max = list.get(i);
            }

        }
        hight.setText("最高温度 : "+max);
        lower.setText("最低温度 : "+min);

        //按下按键实现的功能
        /*button_data1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //数据传入端口
                dynamicLineChartManager1.addEntry((int) (Math.random() * 100));
            }
        });*/
    }

}
